/* 
 * Cashbox is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import <Cashbox/CBAccountPluginMacros.h>
#import <Cashbox/CBAttributeInfo.h>
#import <Cashbox/CBAttribute.h>
#import "CBCreditTransaction.h"

@interface CBTransaction (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber *)primitiveCharge;
- (void)setPrimitiveCharge:(NSNumber *)value;
- (NSNumber *)primitivePayment;
- (void)setPrimitivePayment:(NSNumber *)value;
@end

@implementation CBCreditTransaction

CBDefineTransactionTypes(
	CBDefineAttribute(@"charge", @"Charge", CBCurrencyAttribute, CBNoOptions, CBNegativeCashflowRole, CBPositiveCashflowRole, @"51"),
	CBDefineAttribute(@"payment", @"Payment", CBCurrencyAttribute, CBNoOptions, CBPositiveCashflowRole, CBNegativeCashflowRole, @"52"),
	CBDefineAttribute(@"descrip", @"Description", CBStringAttribute, CBLongTextAttribute, CBDescriptionRole, CBDescriptionRole, @"50")
);

- (long long)internalTotal {
	return [self charge] - [self payment];
}


#pragma mark getters/setters
// ----------------------------------------------------------------------------------------------------
// getters/setters
// ----------------------------------------------------------------------------------------------------

@dynamic descrip;

- (long long)charge {
    NSNumber * tmpValue;
    [self willAccessValueForKey:@"charge"];
    tmpValue = [self primitiveCharge];
    [self didAccessValueForKey:@"charge"];
    return (tmpValue == nil) ? 0 : [tmpValue longLongValue];
}
- (void)setCharge:(long long)charge {
	NSNumber *temp = [[NSNumber alloc] initWithLongLong:charge];
    [self willChangeValueForKey:@"charge"];
    [self setPrimitiveCharge:temp];
    [self didChangeValueForKey:@"charge"];
	[temp release];
}

- (long long)payment {
    NSNumber * tmpValue;
    [self willAccessValueForKey:@"payment"];
    tmpValue = [self primitivePayment];
    [self didAccessValueForKey:@"payment"];
    return (tmpValue == nil) ? 0 : [tmpValue longLongValue];
}
- (void)setPayment:(long long)payment {
	NSNumber *temp = [[NSNumber alloc] initWithLongLong:payment];
    [self willChangeValueForKey:@"payment"];
    [self setPrimitivePayment:temp];
    [self didChangeValueForKey:@"payment"];
	[temp release];
}

- (void)setNilValueForKey:(NSString *)key {
    if ([key isEqualToString:@"credit"] ||
		[key isEqualToString:@"payment"]) {
		[self setValue:[NSNumber numberWithInt:0] forKey:key];
	}
}

@end
